$(document).ready(function() {
	initSearchData();
});
var SEARCH_GOODS = 1; //商品名称
var SEARCH_TENANT_NAME = 2; //租户名称
var SEARCH_STOREHOUSE_ID = 4; //库房名称(ID)
var SEARCH_ISVERIFY = 8; //审核状态
var SEARCH_ISCHARGE = 16; //是否收费
var SEARCH_STOREHOUSE_NAME = 32; //库房名称(NAME)
var SEARCH_STATE = 64; //库房状态
var SEARCH_CREATE_TIME_START = 128; //建单开始时间
var SEARCH_CREATE_TIME_END = 256; //建单开始时间
var SEARCH_DISTRIBUTION_DATE = 512; //配送时间
function buildSearchOptions(searchType, newdata) {
	var data = [{
		"name": "商品名称",
		"type": "NAME",
		"className": "easyui-textbox",
		"id": "input_GOODS_NAME"
	}, {
		"name": "租户名称",
		"type": "TENANT_NAME",
		"className": "easyui-combobox",
		"id": "input_TENANT_NAME"
	}, {
		"name": "库房名称",
		"type": "STOREHOUSE_ID",
		"className": "easyui-combobox",
		"id": "input_STOREHOUSE_ID"
	}, {
		"name": "审核状态",
		"type": "ISVERIFY",
		"className": "easyui-combobox",
		"id": "input_ISVERIFY"
	}, {
		"name": "是否收费",
		"type": "ISCHARGE",
		"className": "easyui-combobox",
		"id": "input_ISCHARGE"
	}, {
		"name": "库房名称",
		"type": "STOREHOUSE_NAME",
		"className": "easyui-combobox",
		"id": "input_STOREHOUSE_NAME"
	}, {
		"name": "库房名称",
		"type": "STATE",
		"className": "easyui-combobox",
		"id": "input_STATE"
	}, {
		"name": "建单开始时间",
		"type": "CREATE_TIME_START",
		"className": "easyui-datebox",
		"id": "input_CREATE_TIME_START"
	}, {
		"name": "建单结束时间",
		"type": "CREATE_TIME_END",
		"className": "easyui-datebox",
		"id": "input_CREATE_TIME_END"
	}, {
		"name": "配送时间",
		"type": "DISTRIBUTION_DATE",
		"className": "easyui-datebox",
		"id": "input_DISTRIBUTION_DATE"
	}];
	var content = "";
	var s;
	for(var i = 0; i < data.length; i++) {
		s = searchType & (1 << i);
		if(s != 0) {
			content += "<td>" + data[i].name + "</td><td><input class=\"" + data[i].className + "\" name=\"" + data[i].type + "\" id=\"" + data[i].id + "\"/></td>";
		}
	}
	if(newdata.length > 0) {
		for(var i = 0; i < newdata.length; i++) {
			content += "<td>" + newdata[i].name + "</td><td><input class=\"" + newdata[i].className + "\" name=\"" + newdata[i].type + "\" id=\"" + newdata[i].id + "\"/></td>";
		}
	}
	var contain = "<div id=\"p\" class=\"easyui-panel\" title=\"查询\" style=\"width:100%;height:100%;\"><table cellspacing=\"4\" style=\"margin-top: 10px;\"><tr>" + content;
	contain += "<td><a href=\"#\" class=\"easyui-linkbutton\" onclick=\"doSearch()\" data-options=\"iconCls:'icon-search'\" style=\"width:80px;\">查询</a></td></tr></table></div>";
	return contain;
}

function initSearchData() {
	if($('#input_STOREHOUSE_ID') != undefined) {
		$('#input_STOREHOUSE_ID').combobox({
			valueField: 'ID',
			textField: 'NAME',
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			url: RemoteLinkAddress + '/frozen/storehouse/listall.do',
		});
	}
	if($('#input_STOREHOUSE_NAME') != undefined) {
		$('#input_STOREHOUSE_NAME').combobox({
			valueField: 'NAME',
			textField: 'NAME',
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			url: RemoteLinkAddress + '/frozen/storehouse/listall.do',
		});
	}
	if($('#input_ISVERIFY') != undefined) {
		$('#input_ISVERIFY').combobox({
			valueField: 'value',
			textField: 'text',
			panelHeight: 'auto',
			data: [{
				"value": "1",
				"text": "已审核"
			}, {
				"value": "0",
				"text": "未审核"
			}]
		});
	}
	if($('#input_ISCHARGE') != undefined) {
		$('#input_ISCHARGE').combobox({ //是否收费
			valueField: 'ID',
			textField: 'NAME',
			panelHeight: 'auto',
			data: [{
				"ID": "0",
				"NAME": "否"
			}, {
				"ID": "1",
				"NAME": "是"
			}]
		});
	}
	if($("#input_TENANT_NAME") != undefined) {
		$('#input_TENANT_NAME').combobox({
			valueField: 'TENANT_NAME',
			textField: 'TENANT_NAME',
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			url: RemoteLinkAddress + '/frozen/tenant/listall.do',
			onChange: function(newValue, oldValue) {
				if(!isEnglish(newValue)) {
					return;
				}
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/frozen/tenant/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			}
		});
	}
	if($('#input_STATE') != undefined) {
		$('#input_STATE').combobox({
			valueField: 'ID',
			textField: 'NAME',
			panelHeight: 'auto',
			data: [{
				"ID": "在用",
				"NAME": "在用"
			}, {
				"ID": "停用",
				"NAME": "停用"
			}, {
				"ID": "包库",
				"NAME": "包库"
			}]
		});
	}

}

function processSearchCondition() {
	if($("#div_search") == undefined) {
		console.log("请检查ID为div_search是否存在");
		return "";
	}
	var searchCondition = "";
	$("#div_search input[name]").each(function(index) {
		if($(this).val().trim().length != 0) {
			if(searchCondition == "") {
				searchCondition = $(this).attr("name") + "=" + $(this).val().trim();
			} else {
				searchCondition += "&" + $(this).attr("name") + "=" + $(this).val().trim();
			}
		}
	});
	return searchCondition;
}